package com.zz.adminv1.controller;

import java.util.List;
import java.util.Map;

import com.zz.common.controller.BaseController;
import com.zz.common.domain.DictDO;
import com.zz.common.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zz.adminv1.domain.ImageDO;
import com.zz.adminv1.service.ImageService;
import com.zz.common.utils.PageUtils;
import com.zz.common.utils.Query;
import com.zz.common.utils.R;

/**
 * 
 * 
 * @author bsea
 * @email yinyouhai@aliyun.com
 * @date 2021-04-22 21:34:45
 */
 
@Controller
@RequestMapping("/adminv1/image")
public class ImageController extends BaseController {
	@Autowired
	private ImageService imageService;
	@Autowired
	private DictService dictService;
	
	@GetMapping()
	@RequiresPermissions("adminv1:image:image")
	String Image(){
	    return "adminv1/image/image";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("adminv1:image:image")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ImageDO> imageList = imageService.list(query);
		int total = imageService.count(query);
		PageUtils pageUtils = new PageUtils(imageList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("adminv1:image:add")
	String add(){
	    return "adminv1/image/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("adminv1:image:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		ImageDO image = imageService.get(id);
		model.addAttribute("image", image);
	    return "adminv1/image/edit";
	}

	@ResponseBody
	@GetMapping("/category/{id}")
	DictDO getCategoryVal(@PathVariable("id") Integer id){
		// id 是什么？这个id是图片主键
		// 根据id,到图片表里面把对应的一行数据查询出来，并且把这行数据封装在image对象里面
		// select * from adminv1_image where id= ?
		//select `id`,`title`,`url`,`category` from adminv1_image where id = #{value}
		ImageDO image = imageService.get(id);
//select
//		`id`,`name`,`value`,`type`,`description`,`sort`,`parent_id`,`create_by`,`create_date`,`update_by`,`update_date`,`remarks`,`del_flag`
//		from sys_dict where type=adminv1_image_type
		List<DictDO> dictDOS = dictService.listByType("adminv1_image_type");
		String type = image.getCategory();
		DictDO resobj=new DictDO();
		for (DictDO dictDO:dictDOS){
			if(type.equals(dictDO.getValue())){
				resobj=dictDO;
			}
		}
		return resobj;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("adminv1:image:add")
	public R save( ImageDO image){
		long userid=getUserId();
		if(imageService.save(image)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("adminv1:image:edit")
	public R update( ImageDO image){
		imageService.update(image);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("adminv1:image:remove")
	public R remove( Integer id){
		if(imageService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("adminv1:image:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		imageService.batchRemove(ids);
		return R.ok();
	}
	
}
